import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { formatBeijingISO } from '../common/date.util';

@Injectable()
export class PersonaService {
  constructor(private prisma: PrismaService) {}

  // Get active persona list
  async listActive() {
    const list = await this.prisma.persona.findMany({
      where: { isActive: true, isPublic: true },
      select: {
        id: true,
        name: true,
        displayName: true,
        description: true,
        avatar: true,
        voiceId: true,
        language: true,
        skills: true,
        createdAt: true,
        updatedAt: true,
      },
      orderBy: { createdAt: 'desc' },
    });
    return list.map((p) => ({
      ...p,
      createdAt: formatBeijingISO(p.createdAt as unknown as Date),
      updatedAt: formatBeijingISO(p.updatedAt as unknown as Date),
    }));
  }

  // Get persona details by ID
  async getById(id: string) {
    const persona = await this.prisma.persona.findFirst({
      where: { id, isActive: true },
    });
    if (!persona) throw new NotFoundException('Persona not found');
    return {
      ...persona,
      createdAt: formatBeijingISO(persona.createdAt as unknown as Date),
      updatedAt: formatBeijingISO(persona.updatedAt as unknown as Date),
    } as any;
  }
}